package com.scgl.zuzhi.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scgl.zuzhi.mapper.ScglDepartmentMapper;
import com.scgl.zuzhi.domain.ScglDepartment;
import com.scgl.zuzhi.service.IScglDepartmentService;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class ScglDepartmentServiceImpl implements IScglDepartmentService
{
    @Autowired
    private ScglDepartmentMapper scglDepartmentMapper;

    /**
     * 查询部门
     *
     * @param id 部门主键
     * @return 部门
     */
    @Override
    public ScglDepartment selectScglDepartmentById(Long id)
    {
        return scglDepartmentMapper.selectScglDepartmentById(id);
    }

    /**
     * 查询部门列表
     *
     * @param scglDepartment 部门
     * @return 部门
     */

    @Override
    public List<ScglDepartment> selectScglDepartmentList(ScglDepartment scglDepartment) {
        List<ScglDepartment> selectScglDepartmentList = scglDepartmentMapper.selectScglDepartmentList(scglDepartment);
        List<ScglDepartment> scglDepartmentTree = buildDepartment(selectScglDepartmentList);
        return scglDepartmentTree;
    }


    private List<ScglDepartment> buildDepartment(List<ScglDepartment> scglDepartmentList) {
        List<ScglDepartment> tree = new ArrayList<>();
        for (ScglDepartment scglDepartment : scglDepartmentList){
            if(scglDepartment.getParentId() == 0){
                tree.add(getChild(scglDepartment, scglDepartmentList));
            }
        }
        return tree;
    }

    private ScglDepartment getChild(ScglDepartment scglDepartment, List<ScglDepartment> scglDepartmentList) {
        scglDepartment.setChildren(new ArrayList<ScglDepartment>());
        for (ScglDepartment per : scglDepartmentList){
            if(per.getParentId().longValue()==scglDepartment.getId().longValue()){
                scglDepartment.getChildren().add(getChild(per, scglDepartmentList));
            }
        }
        System.out.println("======================================="+scglDepartment);
        System.out.println("======================================="+scglDepartment.getChildren());
        return scglDepartment;
    }
//    @Override
//    public List<ScglDepartment> selectScglDepartmentList(ScglDepartment scglDepartment)
//    {
//        return scglDepartmentMapper.selectScglDepartmentList(scglDepartment);
//    }

    /**
     * 新增部门
     *
     * @param scglDepartment 部门
     * @return 结果
     */
    @Override
    public int insertScglDepartment(ScglDepartment scglDepartment)
    {
        return scglDepartmentMapper.insertScglDepartment(scglDepartment);
    }

    /**
     * 修改部门
     *
     * @param scglDepartment 部门
     * @return 结果
     */
    @Override
    public int updateScglDepartment(ScglDepartment scglDepartment)
    {
        return scglDepartmentMapper.updateScglDepartment(scglDepartment);
    }

    /**
     * 批量删除部门
     *
     * @param ids 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteScglDepartmentByIds(Long[] ids)
    {
        return scglDepartmentMapper.deleteScglDepartmentByIds(ids);
    }

    /**
     * 删除部门信息
     *
     * @param id 部门主键
     * @return 结果
     */
    @Override
    public int deleteScglDepartmentById(Long id)
    {
        return scglDepartmentMapper.deleteScglDepartmentById(id);
    }
}
